var modal = {};
modal.alert = function(content, btnText='确定', callback){
    layer.open({
        content:content,
        btn:[btnText],
        yes:function(index){
            layer.close(index);
            if(typeof callback == "function"){
                callback();
            }
        }
    });
};

modal.msg = function (content, times=2) {
    layer.open({
        content:content,
        skin:"msg",
        time:times
    })
};

modal.confirm = function (content, confirmCallBack) {

    layer.open({
        content: content,
        btn: ['确认', '取消'],
        yes: function(index){
            confirmCallBack();
            layer.close(index);
        },
        no:function (index) {
            layer.close(index);
        }
    });
};

modal.loading = function (content) {
    return layer.open({
        type: 2,
        shadeClose:false,
        content: content
    });
};

modal.close = function (index) {
    layer.close(index);
};

modal.showCode = function () {
    $(".showCode").show();
};
$("html").on('click','.showCodeClose',function () {
    $(".showCode").hide();
});

modal.ajax = function (url,data,successCallBack,errorCallBack,type,obj) {
    if(type!=2){
        var loadingTag = modal.loading();
    }
    if(type==2){
        if(obj.hasClass('ajaxIng')){
            // modal.msg('请稍等一会儿！');
            return false;
        }
    }
    $.ajax({
        type:'post',
        url:url,
        data:data,
        cache:false,
        dataType:'JSON',
        beforeSend:function () {
            if(type==2){
                obj.addClass('ajaxIng');
            }
        },
        success:function(res){

            if(res.status=='-1'){
                modal.msg(res.info);
                setTimeout(function () {
                    window.location.href=res.data.url;
                },1000);
            }else{
                successCallBack(res);
            }
            if(type!=2){
                modal.close(loadingTag);
            }else{
                obj.removeClass('ajaxIng');
            }
        },
        error:function(e){
            if(type!=2){
                modal.close(loadingTag);
            }else{
                obj.removeClass('ajaxIng');
            }
            errorCallBack(e);
        }
    });
};